package entity;

import settings.*;

import java.io.IOException;
import java.util.*;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;

import exception.IllegalInput;

/**
 * This class is inherit all the private attribute and all the functions from CashFlow Class
 * @author A0098808L Yuen Shu Hui
 */
@XmlRootElement
public class Income extends CashFlow {

	private final String rootKey = "income";

	/**
	 * @author A0098808L Yuen Shu Hui
	 * Default constructor setting all the attribute to null
	 * @throws IllegalInput 
	 */
	public Income() throws IllegalInput {
		super.setId(null);
		super.setName("NULL");
		super.setCategory(null);
		super.setAmount(0);
		super.setSource(null);
		super.setDate(null);
		super.SetAndVerifyRecurs(false, null);
		super.setRecursParentID(null);
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * Overloaded constructor which take in complete set of private attributes to construct an Income object
	 * @throws TransformerConfigurationException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws JAXBException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 * @throws TransformerException
	 * @throws IllegalInput 
	 */
	public Income(String _name, Category _category, double _amount,
			Source _source, Calendar _date, Boolean _isRecurs,
			Period _recursPeriod, String _recursParentId) throws TransformerConfigurationException,
			InstantiationException, IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, IllegalInput {
		super.setId(IdManager.getInstance().getLastId(rootKey));
		IdManager.getInstance().updateLastId(rootKey);
		super.setName(_name);
		super.setCategory(_category);
		super.setAmount(_amount);
		super.setSource(_source);
		super.setDate(_date);
		super.SetAndVerifyRecurs(_isRecurs, _recursPeriod);
		super.setRecursParentID(_recursParentId);
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * Overloaded constructor which take in an object and unwrap it into Income Object
	 * @param newObject must be either a CashFlow object or an Income Object
	 * @throws IllegalInput 
	 */
	public Income(Object newObject) throws IllegalInput {
		if(newObject.getClass() == Income.class){
			Income temp = (Income) newObject;
			super.setId(temp.getId());
			super.setName(temp.getName());
			super.setCategory(temp.getCategory());
			super.setAmount(temp.getAmount());
			super.setSource(temp.getSource());
			super.setDate(temp.getDate());
			super.SetAndVerifyRecurs(temp.getRecurs(), temp.getRecursPeriod());
			super.setRecursParentID(temp.getRecursParentID());
		}
		else
			throw new IllegalInput("Input object is not an Income object");
	}
}
